package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IWorkOrderInfoService;
import com.yt.order.models.bo.WorkOrderInfoBO;
import com.yt.order.models.bo.WorkOrderInfoSaveBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;

import java.util.List;

/**
 * 工单信息控制器
 *
 * @author yzz
 * @Date 2025-09-03 14:18:55
 */

@RestController
@RequestMapping("/api/workOrderInfo")
@Slf4j
@Api(value = "WorkOrderInfoApiController",tags = "工单信息Api接口")
public class WorkOrderInfoApiController {

    @DubboReference
    private IWorkOrderInfoService workOrderInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<WorkOrderInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        WorkOrderInfoDTO dto = workOrderInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "工单信息列表",notes = "工单信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<WorkOrderInfoDTO>> queryPageList(WorkOrderInfoBO bo){
        Wrapper<WorkOrderInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<WorkOrderInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = workOrderInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "获取所有工单信息", notes = "获取所有工单信息")
    @PostMapping("/listAll")
    public Result<List<WorkOrderInfoDTO>> listAll() {
        List<WorkOrderInfoDTO> workOrderList = workOrderInfoService.selectList(new EntityWrapper<>());
        return new Result<>(workOrderList);
    }

    @ApiOperation(value = "修改工单信息", notes = "修改工单信息")
    @PostMapping("/update")
    public Result update(@RequestBody WorkOrderInfoSaveBO saveBO) {
        log.info("开始更新工单信息，请求参数: {}", saveBO);
        
        if (saveBO.getId() == null) {
            log.warn("更新工单信息失败，ID为空");
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        // 检查工单是否存在
        WorkOrderInfoDTO existing = workOrderInfoService.selectById(saveBO.getId());
        if (existing == null) {
            log.warn("更新工单信息失败，工单不存在，ID: {}", saveBO.getId());
            return new Result<>(CodeEnum.FAIL_100003);
        }
        
        WorkOrderInfoDTO dto = new WorkOrderInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        
        log.info("准备更新工单信息: {}", dto);
        boolean flag = workOrderInfoService.updateById(dto);
        
        if (flag) {
            log.info("工单信息更新成功，ID: {}", saveBO.getId());
            return new Result<>();
        } else {
            log.error("工单信息更新失败，ID: {}", saveBO.getId());
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除工单信息",notes = "删除工单信息")
    @ApiImplicitParam(name = "id",value = "工单信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = workOrderInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "添加工单信息", notes = "添加工单信息")
    @PostMapping("/add")
    public Result add(@RequestBody WorkOrderInfoSaveBO saveBO) {
        log.info("开始添加工单信息，请求参数: {}", saveBO);
        boolean flag = workOrderInfoService.add(saveBO);
        if (flag) {
            log.info("添加工单信息成功");
            return new Result<>();
        } else {
            log.error("添加工单信息失败");
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    /**
     * 分配订单给接单员
     * @param id 订单ID
     * @param dispatcherId 调度员ID
     * @param employeeId 接单员ID
     * @return 分配结果
     */
    @PostMapping("/assign")
    @ApiOperation(value = "分配订单给接单员", notes = "分配订单给接单员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "dispatcherId", value = "调度员ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "employeeId", value = "接单员ID", required = true, paramType = "query", dataType = "Long")
    })
    public Result assignOrder(@RequestParam("id") Long id,
                              @RequestParam("dispatcherId") Long dispatcherId,
                              @RequestParam("employeeId") Long employeeId) {
        WorkOrderInfoDTO dto = new WorkOrderInfoDTO();
        dto.setId(id);
        dto.setDispatcherId(dispatcherId);
        dto.setEmployeeId(employeeId);
        dto.setStatus(1); // 设置为处理中状态

        boolean flag = workOrderInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

}
